/**
 * 科目详情对话框
 */
var ZxKemuInfoDlg = {
    data: {
        btyZxKemuId: "",
        name: ""
    }
};

layui.use(['layer','form', 'table', 'admin', 'ax','upload'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var layer = layui.layer;

    /**
     * 管理
     */
    var ZxKemuKey = {
        tableId: "zxKemuKeyTable"
    };

    /**
     * 初始化表格的列
     */
    ZxKemuKey.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '',width:100},
            {field: 'secondLevelName', sort: true, title: '二级科目',width:300},
            {field: 'keyword', sort: true, title: '关键字',width:300},
            {field:'isJgc',sort: true,title: "甲供材",width: 160, align: "center",templet:function(data){
                    switch (data.isJgc) {
                        case "1":return "是";break;
                        case "0":return "否";break;
                        default :return "未设置";break;
                    }
                }},
            {align: 'center', toolbar: '#tableBar', title: '操作',width:200}
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ZxKemuKey.tableId,
        url: Feng.ctxPath + '/dgdzxKemuKey/list',
        page: true,
        height: "full-158",
        width:800,
        limit: 90,
        cellMinWidth: 100,
        cols: ZxKemuKey.initColumn()
    });

    /**
     * 点击查询按钮
     */
    ZxKemuKey.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(ZxKemuKey.tableId, {where: queryData});
    };

    /**
     * 弹出添加对话框
     */
    ZxKemuKey.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/dgdzxKemuKey/add';
    };

    /**
     * 导出excel按钮
     */
    ZxKemuKey.exportExcel = function () {
        var checkRows = table.checkStatus(ZxKemuKey.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ZxKemuKey.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/dgdzxKemuKey/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ZxKemuKey.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/dgdzxKemuKey/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ZxKemuKey.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };



    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ZxKemuKey.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ZxKemuKey.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ZxKemuKey.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ZxKemuKey.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ZxKemuKey.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ZxKemuKey.onDeleteItem(data);
        }
    });

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/dgdzxKemuKey/addItem", function (data) {
            if (data.code == 200) {
                $("#keyword").val("");
                Feng.success("添加成功！");
                table.reload(ZxKemuKey.tableId);
            }else {
                Feng.error(data.message)
            }
        }, function (data) {
            Feng.error("添加失败！" + data.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    // 点击科目时
    $('#btyZxKemuIdView').click(function () {
        var formName = encodeURIComponent("parent.ZxKemuInfoDlg.data.name");
        var formId = encodeURIComponent("parent.ZxKemuInfoDlg.data.btyZxKemuId");
        var treeUrl = encodeURIComponent("/dgdzxKemu/tree");

        layer.open({
            type: 2,
            title: '科目选择（请双击选择）',
            area: ['50%', '80%'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#btyZxKemuId").val(ZxKemuInfoDlg.data.btyZxKemuId);
                $("#btyZxKemuIdView").val(ZxKemuInfoDlg.data.name);
            }
        });
    });

    //批量设置甲供材
    $('#btnIsJgc').click(function () {
        ZxKemuKey.setJgc();
    });

    //批量设置甲供材
    $('#btnqxJgc').click(function () {
        ZxKemuKey.qxJgc();
    });


    ZxKemuKey.setJgc = function () {
        var checkRows = table.checkStatus(ZxKemuKey.tableId);
        var data = checkRows.data;
        var ids = "";
        if (checkRows.data.length === 0) {
            Feng.error("请选择关键字，才能批量设置！");
            return false;
        }else {
            for (var i in data) {
                if(ids==""){
                    ids=data[i].id;
                }else {
                    ids+= ","+data[i].id;
                }
            }
        }

        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/dgdzxKemuKey/setJgc", function (data) {
                if(data.code==200){
                    Feng.success("设置成功!");
                    table.reload(ZxKemuKey.tableId);
                }else {
                    Feng.error(data.message);
                }
            });
            ajax.set("isJgc", "1");
            ajax.set("ids", ids);
            ajax.start();
        };
        Feng.confirm("是否设置甲供材?", operation);
    };
    ZxKemuKey.qxJgc = function () {
        var checkRows = table.checkStatus(ZxKemuKey.tableId);
        var data = checkRows.data;
        var ids = "";
        if (checkRows.data.length === 0) {
            Feng.error("请选择关键字，才能批量设置！");
            return false;
        }else {
            for (var i in data) {
                if(ids==""){
                    ids=data[i].id;
                }else {
                    ids+= ","+data[i].id;
                }
            }
        }

        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/dgdzxKemuKey/setJgc", function (data) {
                if(data.code==200){
                    Feng.success("设置成功!");
                    table.reload(ZxKemuKey.tableId);
                }else {
                    Feng.error(data.message);
                }
            });
            ajax.set("isJgc", "0");
            ajax.set("ids", ids);
            ajax.start();
        };
        Feng.confirm("是否取消甲供材?", operation);
    };
});
